package com.mzw.mzwclientsdk.client;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.mzw.mzwclientsdk.model.Message;
import com.mzw.mzwclientsdk.model.SongName;
import com.mzw.mzwclientsdk.model.User;
import com.mzw.mzwclientsdk.model.Word;


import java.util.HashMap;
import java.util.Map;

import static com.mzw.mzwclientsdk.utils.SignUtils.genSign;


/**
 * 调用第三方客户端
 *
 * @author L1nAn
 */
public class MzwClient {

    // 网关地址
    public static final  String GATEWAY_HOST = "http://localhost:8090";
    private String accessKey;
    private String secretKey;
    public MzwClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    public String getNameByGet(String name) {
        // 封装参数
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        // 转发
        String result = HttpUtil.get(GATEWAY_HOST + "/api/name/get", paramMap);
        System.out.println(result);
        return result;
    }

    public String getNameByPost(String name) {
        // 封装参数
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        //转发
        String result = HttpUtil.post(GATEWAY_HOST + "/api/name/", paramMap);
        System.out.println(result);
        return result;
    }

    /**
     * 添加请求头信息
     * @param body
     * @return
     */
    private Map<String, String> getHeaderMap(String body) {
        Map<String, String> hashMap = new HashMap<>();
        // 调用标识
        hashMap.put("accessKey", accessKey);
        // secretKey 不能再请求头中传递
//        hashMap.put("secretKey", secretKey);
        // 随机数
        hashMap.put("nonce", RandomUtil.randomNumbers(4));
        hashMap.put("body", body);
        // 时间戳
        hashMap.put("timestamp", String.valueOf(System.currentTimeMillis() / 100));
        // 加密密码
        hashMap.put("sign", genSign(body, secretKey));
        return hashMap;
    }

    public String getUsernameByPost(User user) {
        String json = JSONUtil.toJsonStr(user);
        HttpResponse httpResponse = HttpRequest.post(GATEWAY_HOST + "/api/name/user")
                .addHeaders(getHeaderMap(json))
                .body(json)
                .execute();
        // 响应状态码
        System.out.println(httpResponse.getStatus());
        // 获取结果
        String result = httpResponse.body();
        System.out.println(result);
        return result;
    }

    // 获取歌曲
    public String getSongName(SongName songName) {

        String json = JSONUtil.toJsonStr(songName);
        HttpResponse httpResponse = HttpRequest.post(GATEWAY_HOST + "/api/song/name")
                .addHeaders(getHeaderMap(json))
                .body(json)
                .execute();
        // 响应肯定是有状态码
        System.out.println(httpResponse.getStatus());
        String result = httpResponse.body();
        System.out.println(result);
        return result;
    }

    // 翻译单词
    public String getTranslation(Word word) {
        // 封装参数

        String json = JSONUtil.toJsonStr(word);
        HttpResponse httpResponse = HttpRequest.post(GATEWAY_HOST + "/api/word/translation")
                .addHeaders(getHeaderMap(json))
                .body(json)
                .execute();
        // 响应肯定是有状态码
        System.out.println(httpResponse.getStatus());
        String result = httpResponse.body();
        System.out.println(result);
        return result;
    }
    // 随机产生土味情话
    public String getLoveWords(Message message) {
        // 封装参数

        String json = JSONUtil.toJsonStr(message);
        HttpResponse httpResponse = HttpRequest.post(GATEWAY_HOST + "/api/message/getWords")
                .addHeaders(getHeaderMap(json))
                .body(json)
                .execute();
        // 响应肯定是有状态码
        System.out.println(httpResponse.getStatus());
        String result = httpResponse.body();
        System.out.println(result);
        return result;
    }
}
